package org.gwtEss.gwtExtensions;

import org.gwtEss.client.GwtEss;
import org.gwtEss.other.DivPanel;

import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;

public class EnhancedFlexTable extends FlexEventsTable {

	public EnhancedFlexTable() {
		super();
	}
	private DivPanel getExistingWidget(int row, int column){
		Widget cellDiv = null;
		if (getRowCount() > row && getCellCount(row) > column)
			cellDiv = super.getWidget(row, column);
		else return null; 
		return (DivPanel)cellDiv;
	}
	@Override
	public void setWidget(int row, int column, Widget widget) {
		DivPanel cellDiv = getExistingWidget(row, column);
		if (cellDiv == null){
			cellDiv = new DivPanel(widget);
			cellDiv.addStyleName("divClip");
			super.setWidget(row, column, cellDiv);
		}else 
			((DivPanel)cellDiv).setWidget(widget);
	}
	@Override
	public void setText(int row, int column, String text) {
		DivPanel cellDiv = getExistingWidget(row, column);
		if (cellDiv == null){
			cellDiv = new DivPanel(text);
			cellDiv.addStyleName("divClip");
			super.setWidget(row, column, cellDiv);
		}else 
			cellDiv.setText(text);
		cellDiv.setText(text);
	}
	@Override
	public Widget getWidget(int row, int column) {
		DivPanel widget = getExistingWidget(row, column);
		return widget==null?null:widget.getWidget();
//		return ((DivPanel)super.getWidget(row, column)).getWidget();
	}
	@Override
	public String getText(int row, int column) {
		return getExistingWidget(row, column).getText();
//		return ((DivPanel)super.getWidget(row, column)).getText();
	}
	
	public void setColumnWidth(int column, int width){
		
		DivPanel w;
		for (int i=0; i<getRowCount(); i++){
			w = getExistingWidget(i, column);
			if (w != null)
				w.setWidth(width + "px");
		}
		
	}
}
